

* Norwegian election study
* Match following:
/*
v012 foreign aid 							outcome25
v016 environmental protection				outcome21
v019 decrease taxes							outcome4
v020 industry too much influence			outcome9
v032 immigrants								outcome26
v035 eu membership							outcome30
v036 climate change 						outcome22
v039 reduce economic differences			outcome7
v044 strengthen armed forces				outcome2
v092 abortion								outcome16
v095 allow commercial private schools 		outcome11
v184 more freedom companies					outcome29
v186 privatize								outcome6
v201 social benefits 						outcome3

*/



clear all

capture program drop store_est
program define store_est
	local indepvar = "`1'"
	matrix mat_`indepvar'_n[`2',`3']=e(N)
	matrix mat_`indepvar'_beta[`2',`3']=_b[temp_x]
	matrix mat_`indepvar'_se[`2',`3']=_se[temp_x]
	matrix mat_`indepvar'_var[`2',`3']=_se[temp_x]^2
	matrix mat_`indepvar'_t[`2',`3']=_b[temp_x]/_se[temp_x]
end

* SE of effect estimate average
capture program drop calc_SE
program define calc_SE

	local indepvar = "`1'"
	local model = `2'

	* Total sum of variances
	egen sum_var_`indepvar'_`model'=sum(mat_`indepvar'_var`model')
	
	* Second term
	local tempsum = 0
	local actualn = 0
	forvalues i=1(1)`3' {
		if mat_`indepvar'_se[`i',`model']!=. {
			local actualn = `actualn'+1
		}
		forvalues j=1(1)`3' {
			if `i'!=`j' & mat_`indepvar'_se[`i',`model']!=. & mat_`indepvar'_se[`j',`model']!=. {
				local tempsum = `tempsum' + `4'corr_mat[`i',`j'] * mat_`indepvar'_se[`i',`model'] * ///
					mat_`indepvar'_se[`j',`model'] * `4'flip_`indepvar'[`i']  * `4'flip_`indepvar'[`j']
			}
		}
	}
	
	* Total standard error
	gen se_`indepvar'_`model' = sqrt(`tempsum' + sum_var_`indepvar'_`model'[1])*(1/`actualn') in 1

end

cd "C:\Userdata\Shared\Dofiles\PrelDoFiles\PrelRafael\Quantifying bias\replication_code"
use "C:\Userdata\Shared\Dofiles\PrelDoFiles\PrelRafael\Quantifying bias\ext_data\NSD1487_ElectionStudy2009.dta" 



* Dependents
gen foreign_aid = v012
	replace foreign_aid=. if foreign_aid>3
gen environmental_protection = v016
	replace environmental_protection=. if environmental_protection>5
gen taxes = v019
	replace taxes=. if taxes>5
gen industry_influence = v020
	replace industry_influence=. if industry_influence>5
gen immigration = v032
	replace immigration=. if immigration>10
gen eu = v035
	replace eu=. if eu>10
gen climate_change = v036
	replace climate_change=. if climate_change>10
gen reduce_inequality = v039
	replace reduce_inequality=. if reduce_inequality>5
gen armed_forces = v044
	replace armed_forces=. if armed_forces>5
gen abortion = v092
	replace abortion=. if abortion>4
gen private_schools = v095
	replace private_schools=. if private_schools>5
gen more_freedom_companies = v184
	replace more_freedom_companies=. if more_freedom_companies>5
gen privatize = v186
	replace privatize=. if privatize>5
gen benefits = v201
	replace benefits=. if benefits>3



local i=1
foreach v of varlist foreign_aid - benefits {
	gen outcome`i' = `v'
	local i = `i' + 1
}


* Gen independents
gen educationYears=.
	replace educationYears=6 if v295==1
	replace educationYears=9 if v295==2
	replace educationYears=10 if v295==3
	replace educationYears=12 if v295==4
	replace educationYears=12 if v295==5
	replace educationYears=15 if v295==6
	replace educationYears=17 if v295==7
	replace educationYears=20 if v295==8
gen bin_edu=.
	replace bin_edu=0 if educationYears<15
	replace bin_edu=1 if educationYears>14 & educationYears!=.
gen income=v296
replace income=. if income>6000

* Gen controls
gen sex = v006
gen age = v004
gen municipality = v002
gen occupation = floor(v263/100)
	replace occupation=199 if occupation==.


* Inclusion
gen include_indicator=.
replace include_indicator=1 if sex!=. & age!=. & municipality!=. & occupation!=. & income!= . & educationYears != .

* Cleanup
keep outcome1 - include_indicator


* Create necessary matrices for storage
foreach indepvar in bin_edu educationYears income {
	matrix mat_`indepvar'_beta=J(14,2,.)
	matrix mat_`indepvar'_n=J(14,2,.)
	matrix mat_`indepvar'_se=J(14,2,.)
	matrix mat_`indepvar'_var=J(14,2,.)
	matrix mat_`indepvar'_t=J(14,2,.)
}

* Correlation matrix
pwcorr outcome1-outcome14 if include_indicator==1
matrix corr_mat=r(C)


* Run analyses
local i=1
foreach depvar of varlist outcome1-outcome14 {
	
	foreach indepvar in educationYears bin_edu income {
	
		di `i' ", `depvar' `indepvar'"
	
		qui:su `depvar' if `indepvar'!=. & include_indicator!=.
		qui:gen temp_y = (`depvar'-r(mean))/r(sd)
		qui:su `indepvar' if `depvar'!=. & include_indicator!=.
		qui:gen temp_x = (`indepvar'-r(mean))/r(sd)
	
		* Empty model
		qui: reghdfe temp_y temp_x c.sex##i.age if include_indicator==1, noabsorb
		store_est `indepvar' `i' 1
	
		* Naive model
		if "`indepvar'" == "bin_edu"|"`indepvar'" == "educationYears" {
			qui: reghdfe temp_y temp_x c.sex##i.age i.occupation income if include_indicator==1, absorb(municipality)
			store_est `indepvar' `i' 2
		}
		else {
			qui: reghdfe temp_y temp_x c.sex##i.age i.occupation educationYears if include_indicator==1, absorb(municipality)
			store_est `indepvar' `i' 2
		}
		
		drop temp_x temp_y
		
	}
	local i=`i'+1
}

drop _all
foreach indepvar in bin_edu educationYears income {

	* Turn matrices into variables
	svmat mat_`indepvar'_n
	svmat mat_`indepvar'_beta
	svmat mat_`indepvar'_t
	svmat mat_`indepvar'_se
	svmat mat_`indepvar'_var
	
	* Flip vector
	gen flip_`indepvar' = 1
	replace flip_`indepvar' = -1 if mat_`indepvar'_beta1<0
	
	* Flipped betas
	gen `indepvar'_1 = mat_`indepvar'_beta1 * flip_`indepvar'
	gen `indepvar'_2 = mat_`indepvar'_beta2 * flip_`indepvar'
	
	* Means
	egen m_`indepvar'_1 = mean(`indepvar'_1)
	egen m_`indepvar'_2 = mean(`indepvar'_2)
	gen `indepvar'_m =.
	replace `indepvar'_m = m_`indepvar'_1[1] in 1
	replace `indepvar'_m = m_`indepvar'_2[1] in 2
	drop m_*
	
	* Standard errors
	calc_SE `indepvar' 1 14
	calc_SE `indepvar' 2 14
	gen `indepvar'_se =.
	replace `indepvar'_se = se_`indepvar'_1[1] in 1
	replace `indepvar'_se = se_`indepvar'_2[1] in 2

	gen `indepvar'_Nmin = .
	gen `indepvar'_Nmax = .
	gen `indepvar'_delta = .
	gen `indepvar'_tdelta = .
	forvalues i = 1(1)2 {
		egen Nmin_`indepvar'_`i' = min(mat_`indepvar'_n`i')
		egen Nmax_`indepvar'_`i' = max(mat_`indepvar'_n`i')
		replace `indepvar'_Nmin = Nmin_`indepvar'_`i'[1] in `i'
		replace `indepvar'_Nmax = Nmax_`indepvar'_`i'[1] in `i'
		drop Nmin_`indepvar'_`i' Nmax_`indepvar'_`i'
	}
	replace `indepvar'_delta = `indepvar'_m[2] - `indepvar'_m[1] in 2
	replace `indepvar'_tdelta = `indepvar'_delta[2]/sqrt(`indepvar'_se[1]^2 + `indepvar'_se[2]^2) in 2
	
}

keep *_m *_se *_Nmin *Nmax *_delta *_tdelta
keep if [_n]<3
rename * NES_*
save NES_est.dta, replace


